package com.didi.gulimall.order.web;

import com.alibaba.fastjson.JSON;
import com.didi.gulimall.common.entity.LocalMqEntity;
import com.didi.gulimall.common.to.OrderSuccessVo;
import com.didi.gulimall.common.utils.R;
import com.didi.gulimall.mq.GuliRabbitTemplate;
import com.didi.gulimall.order.service.PayWebService;
import com.didi.gulimall.order.vo.PayAsyncVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @Author 10626
 * @Create 2021/6/7 13:15
 */
@Controller
@Slf4j
@Api("支付控制器")
public class PayWebController {
    @Autowired
    private PayWebService payWebService;

    @Autowired
    private GuliRabbitTemplate guliRabbitTemplate;

    @ResponseBody
    @GetMapping("payOrder")
    public String patOrder(String orderSn) {
        return payWebService.patOrder(orderSn);
    }

    @ResponseBody
    @PostMapping("order/ailipay/verifyNotify")
    public String handleAlipayed(PayAsyncVo vo, HttpServletRequest request) throws Exception {
        return payWebService.asynchronousNotification(vo, request);
    }

    @ResponseBody
    @GetMapping("query/alpay/trade")
    public R queryAlpayTrade(@RequestParam("orderSn") String orderSn) throws Exception {
        return payWebService.queryAlpayTrade(orderSn);
    }

    @ResponseBody
    @GetMapping("cancel/alpay/trade")
    public R cancelAlpayTrade(@RequestParam("orderSn") String orderSn) throws Exception {
        return payWebService.cancelAlpayTrade(orderSn);
    }

    @ResponseBody
    @PostMapping("send/paysuccess")
    public R handleAlipayed() throws Exception {
        LocalMqEntity localMqEntity = new LocalMqEntity();
        OrderSuccessVo orderSuccessVo = new OrderSuccessVo();
        orderSuccessVo.setMemberId(2L);
        localMqEntity.setBody(JSON.toJSONString(orderSuccessVo));
        localMqEntity.setClassType("com.didi.gulimall.common.to.OrderSuccessVo");
        localMqEntity.setExchange("order-event-exchange");
        localMqEntity.setRouteKey("order.paysuccess.#");
        localMqEntity.setId(UUID.randomUUID().toString());
        guliRabbitTemplate.sendConvertAndSend(localMqEntity);
        return R.ok();
    }


}
